package br.edu.ifrn.sgpapp.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.edu.ifrn.sgp.dtos.NoticiaDTO;
import br.edu.ifrn.sgpapp.R;
import br.edu.ifrn.sgpapp.util.Formatador;

public class NoticiasListAdapter extends BaseAdapter {

	private Activity context;
	
	private List<NoticiaDTO> noticias;
	
	public NoticiasListAdapter(Activity context, List<NoticiaDTO> noticias) {
		super();
		this.context = context;
		this.noticias = noticias;
	}
	
	@Override
	public int getCount() {
		return noticias.size();
	}

	@Override
	public NoticiaDTO getItem(int position) {
		return noticias.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	private class ViewHolder {
		TextView txtItem;
		TextView txtItem2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LayoutInflater inflater = context.getLayoutInflater();
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.lista_geral_item, null);
			holder = new ViewHolder();
			holder.txtItem = (TextView) convertView.findViewById(R.id.itemTxt);
			holder.txtItem2 = (TextView) convertView.findViewById(R.id.itemTxt2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.txtItem.setText(noticias.get(position).getTitulo());
		holder.txtItem2.setText(Formatador.getInstance().formatarDataHora(noticias.get(position).getCriadoEm()));

		return convertView;
	}

}
